
/* ********************************************************************* */
/*                                                                       */
/*                       Librairies de Logindiant                          */
/*                                                                       */
/* ********************************************************************* */

#include "Login.h"

/* ********************************************************************* */
/*                                                                       */
/*                        Les Fonctions                                  */
/*                                                                       */
/* ********************************************************************* */

void  initLoginVide(Login*log)
{
    log->identifiant=malloc(sizeof(char)*20);
    log->mdp=malloc(sizeof(char)*20);
    log->type=0;
}

void initLogin(Login * log,char* id,char *mdp, int type)
{
    log->identifiant=malloc(sizeof(char)*20);
    log->mdp=malloc(sizeof(char)*20);
    log->identifiant=id;
    log->mdp=mdp;
    log->type=type;
}

Login * creLogin(char* id,char *mdp, int type)
{
    Login *log=(Login *) malloc(sizeof(Login));
    initLogin(log,id,mdp,type);

    return log ;
}

void detLog(Login ** log)
{
    assert(*log != NULL);

    liblog(*log);
    free(*log);
    *log = NULL ;
}

void liblog(Login *log)
{
    assert(log!=NULL);
    log->identifiant   = NULL;
    log->mdp = NULL;
    log->type = 0;
}

/* ******************************************************************** */
/*																		*/
/*           Accesseurs et Mutateurs de la structure Login              */
/*																		*/
/* ******************************************************************** */

char * getId(const Login *log)
{
    if(log && (log->identifiant!= NULL))
        return log->identifiant;
    else
        return NULL;
}

void setId(Login *log,const char *id)
{
    assert(id!=NULL);
    assert(log!=NULL);
    log->identifiant=(char*)id;
}

char *getMdp(const Login *log)
{
    if(log && (log->mdp != NULL))
        return log->mdp;
    else
        return NULL;
}

void setMdp(Login * log,const char * mdp)
{
    assert(mdp!=NULL);
    assert(log!=NULL);
    log->mdp=(char *)mdp;
}

int getTypeLog(const Login *log)
{
    if(log && (log->type>0))
        return log->type;
    else
        return 0;
}

void setTypeLog(Login * log,const int type)
{
    assert(type>0);
    assert(log!=NULL);
    log->type=type;
}

void affLogin(const Login *log)
{
	if(log!=NULL && log->identifiant!=NULL && log->mdp!=NULL && log->type>0)
	{
		printf("Identifiant de login : %s\n",log->identifiant);
		printf("Mot de passe de login: %s\n" ,log->mdp);
		switch (log->type)
		{
			case 1:
                printf("Type de login  : Etudiant \n");
				break;

			case 2:
                printf("Type de login : Enseniant\n");
				break;

			default:
                printf("aucun de type !!\n");
				break;
		}
	}
	else
	{
		printf("\nAucun Login!!");
	}
}

/* ********************************************************************* */
/*                                                                       */
/*             Fonctions relatives aux gestiond'Log                      */
/*                                                                       */
/* ********************************************************************* */



Login* rchLogParIdMdp(char* identifiant, char*mdp)
{
    FILE* f;
    char* contenu;
    char**tabor, **tab;
    int typelog;

    f = fopen(LOGIN_DB, "r");

    if (f==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", LOGIN_DB);
        exit(EXIT_FAILURE);
    }

    contenu=malloc(sizeof(char)*100);
    while(fgets(contenu,100,f)!=NULL)
    {
        tabor=str_split(contenu,"\n");
        tab=str_split(tabor[0],":");
        typelog =atoi(tab[2]);

        if(strcmp(identifiant,tab[0]) ==0 && strcmp(mdp,tab[1])==0)
        {
            fclose(f);
            free(tabor);
            free(tab);
            return creLogin(identifiant,mdp,typelog);
            /*return creLogin(identifiant,mdp,atoi(tab[2]));*/
        }
        free(tabor);
        free(tab);
    }

    if(!feof(f))
        printf("autre probleme \n");

    fclose(f);
    return NULL;
}

Login *recLoginParId( char* pId)
{
    FILE* f;
    char* contenu, *mdplog;
    int typelog;
    char **tabor, **tab;

    f = fopen(LOGIN_DB, "r");

    if (f==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", LOGIN_DB);
        exit(EXIT_FAILURE);
    }

    contenu=malloc(sizeof(char)*100);
    while(fgets(contenu,100,f)!=NULL)
    {
        tabor=str_split(contenu,"\n");
        tab=str_split(tabor[0],":");

        mdplog = tab[1];
        typelog = atoi(tab[2]);

        if(strcmp(pId,tab[0])==0)
        {
            fclose(f);
            free(tabor);
            free(tab);

            return creLogin(pId, mdplog, typelog);
            /*return creLogin(pId,tab[1],atoi(tab[2]));*/
        }

        free(tabor);
        free(tab);
    }
    free(contenu);

    if(!feof(f))
        printf("autre probleme \n");

    fclose(f);
    return NULL;
}


void savLog( Login *pLog)
{
    FILE* f;
    char *ctype, *chaine;

    f = fopen(LOGIN_DB, "a");
    fclose(f);

    if(recLoginParId(pLog->identifiant) == NULL)
    {
        f = fopen(LOGIN_DB, "a");

        if (f==NULL)
        {
            printf("Erreur lors de l'ouverture de %s\n", LOGIN_DB);
            exit(EXIT_FAILURE);
        }
        ctype=malloc(sizeof(char)*1);
        sprintf(ctype,"%d",pLog->type);

        chaine=calloc(sizeof(char),100);
        strcat(chaine,pLog->identifiant);
        strcat(chaine,":");
        strcat(chaine,pLog->mdp);
        strcat(chaine,":");
        strcat(chaine,ctype);
        strcat(chaine,"\n");
        fputs(chaine, f);
        fclose(f);

        free(ctype);
        free(chaine);
    }
    else
        printf("Login deja present !!!!!\n");
}

/** @brief modifLogin
  *
  * @todo: document this function
  */
int modifLogin(Login*log,Login*log2)
{
    if(supLog(log)==0)
    {
        savLog(log2);
        return  0;
    }
    return  -1;
}

/** @brief supLogParId
  *
  * @todo: document this function
  */
int supLogParId(char id[])
{
    FILE * fIn;
    FILE * fOut;
    char *contenu, *ligne;
    char **tabor, **tab;

    if ((fIn = fopen(LOGIN_DB, "r")) == NULL)
        return EXIT_FAILURE;

    if ((fOut = fopen("texte.tmp", "w")) == NULL)
    {
        fclose(fIn);
        return EXIT_FAILURE;
    }

    contenu=malloc(sizeof(char)*100);
    while (fgets(contenu, 100, fIn))
    {
        ligne=malloc(sizeof(char)*100);
        copStr(&ligne,contenu);
        tabor=str_split(contenu,"\n");
        tab=str_split(tabor[0],":");
        if (strcmp(id,tab[0])!=0)
            fputs(ligne, fOut);

        free(ligne);
        free(tabor);
        free(tab);
    }

    free(contenu);

    fclose(fIn);
    fclose(fOut);

    rename("texte.tmp", LOGIN_DB);
    return 0;
}

/** @brief supLog
  *
  * @todo: document this function
  */
int supLog(Login*log)
{
    assert(log!=NULL);
    return supLogParId(log->identifiant);
}

void affLog_DB()
{
    FILE* f;
    char*contenu;
    char**tabor, **tab;

    f = fopen(LOGIN_DB, "r");

    if (f==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", LOGIN_DB);
        exit(EXIT_FAILURE);
    }

    contenu=calloc(sizeof(char*),100);
    while(fgets(contenu,100,f) != NULL)
    {
        tabor=str_split(contenu,"\n");
        tab=str_split(tabor[0],":");
        affLogin(creLogin(tab[0],tab[1],atoi(tab[2])));

        free(tabor);
        free(tab);
    }

    if(!feof(f))
        printf("autre probleme \n");
    fclose(f);
}

void regLog()
{
    affLog_DB();
}
